/*****************************************************************************
* File Name: USBFS_HID_interface.h
* Version: 1.0
*
* Description: 
* Receives Data from USB and send to PRoC BLE over UART and Receives data 
* from PRoC BLE over UART and sends to USB.
*
* Owner:
* SIRK
*
* Related Document:
*     
*
* Hardware Dependency:
*
* Hardware Dependency:
* CY5670 CySmart USB Dongle
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
*
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/

#ifndef _USB_INTERFACE_H_
#define _USB_INTERFACE_H_

#include "AudioControl.h"

/*****************************************************************************
* MACRO Definition
*****************************************************************************/

/* USB endpoint usage */
#define MOUSE_EP            USBFS_EP1
#define KEYBOARD_EP         USBFS_EP2
#define AUDIO_EP            AUDIO_IN_ENDPOINT
#define CDC_IN_EP           USBFS_EP6
#define UART_OUT_EP         USBFS_EP7
  
/*Set to max size of audio endpoint, others are smaller*/
#define USB_MAX_IN_EP_SIZE  (64u)

/*Max size of out endpoint used for CDC*/
#define USB_MAX_OUT_EP_SIZE (64u)

/*****************************************************************************
* API Constants
*****************************************************************************/

/* UART Source Clock Frequency */
#define SOURCECLK           24000000

/* Divider for Baud Rates */
#define DIVIDER500000       SOURCECLK/(UART_OVER_SAMPLE_COUNT * 500000)
#define BAUDRATE921600      921600
#define BAUDRATE1200        1200

#ifdef EN_GENERIC_BAUDRATE_CONFIG
#define DIVIDER1000000      SOURCECLK/(UART_OVER_SAMPLE_COUNT * 1000000)
#define DIVIDER250000       SOURCECLK/(UART_OVER_SAMPLE_COUNT * 250000)
#define DIVIDER115200       SOURCECLK/(UART_OVER_SAMPLE_COUNT * 115200)
#define DIVIDER57600        SOURCECLK/(UART_OVER_SAMPLE_COUNT * 57600)
#define DIVIDER38400        SOURCECLK/(UART_OVER_SAMPLE_COUNT * 38400)
#define DIVIDER19200        SOURCECLK/(UART_OVER_SAMPLE_COUNT * 19200)
#define DIVIDER9600         SOURCECLK/(UART_OVER_SAMPLE_COUNT * 9600)
#define DIVIDER4800         SOURCECLK/(UART_OVER_SAMPLE_COUNT * 4800)
#define DIVIDER2400         SOURCECLK/(UART_OVER_SAMPLE_COUNT * 2400)
#define DIVIDER1200         SOURCECLK/(UART_OVER_SAMPLE_COUNT * 1200)

#define BAUDRATE460800      460800
#define BAUDRATE230400      230400
#define BAUDRATE115200      115200
#define BAUDRATE57600       57600
#define BAUDRATE38400       38400
#define BAUDRATE19200       19200
#define BAUDRATE9600        9600
#define BAUDRATE4800        4800
#define BAUDRATE2400        2400
#endif

/*****************************************************************************
* Data Type Definition
*****************************************************************************/

/*****************************************************************************
* Enumerated Data Definition
*****************************************************************************/
/* Indexes into the Endpoint_Packet array(intermediate buffer) for packets.*/
typedef enum  _UART_RX_PKT_BUFF_INDEX
{
    CDC_IN_DATA_INDEX,
    MOUSE_DATA_INDEX,
    KEYBOARD_DATA_INDEX,
    MAX_REPORT_TYPE,
}UART_RX_PKT_BUFF_INDEX;

/* State machine implementation for packet decoding of the received data
        over UART.
        There are 3 states in this implementation. 
        1. Receive Packet Header.
        2. Receive packet Length.
        3. Receive Packet OpCode
        4. Receive Payload
*/

/* Various states of the State machine for Rx in UART */
typedef enum _UART_RX_PKT_STATE
{
    STATE_PACKET_HEADER_CODE = 0u,
    STATE_PACKET_LENGTH,
    STATE_PACKET_OPCODE,
    STATE_PACKET_PAYLOAD,
}UART_RX_PKT_STATE;

/*Valid Rx packets from UART goes to respective Endpoint_Packet buffers.*/
typedef struct _EndPoint_Packet
{  
    /*Endpoint number used by packet*/
    uint8  endpoint;
    /*Local buffer meant for the packet - 
            this buffer will hold UART data until corresponding EP is free*/
    uint8  *data;
    /*Current index of data buffer which is pending for USB transfer*/
    uint16 offset;
    /*Total length of packet to be transmitted to USB EP*/
    uint16 datalen;
}Endpoint_Packet; 

/*Op-Codes for each Report Type from PRoC-BLE
    These op-codes are hard-coded and both the micro-controllers on dongle uses these to identify the type of packet
    If the values to be changed, need to be changed in both the projects*/
typedef enum
{
    MOUSE_REPORT         = 0x0461u,
    KEYBOARD_REPORT      = 0x0462u,
    AUDIO_REPORT         = 0x0463u,
    AUDIO_CONTROL_STATUS = 0x0464u,
    AUDIO_SYNC_PACKET    = 0x0465u
}Report_Type;


/*****************************************************************************
* Data Structure Definition
*****************************************************************************/

/*****************************************************************************
* Global Variable Declaration
*****************************************************************************/
/*State m/c for packet detection*/
extern volatile UART_RX_PKT_STATE pktRxState;
/*Byte index of current packet under process*/
extern volatile uint32 curPacketIndex;
/*Flag to indicate if UART OUT data available from USB Interface*/
extern volatile bool isCDCOutDataPresent;
#ifdef RESET_BUF_LEVEL
extern volatile uint16 txDataDropSize;
#endif /*RESET_BUF_LEVEL*/

/*****************************************************************************
* External Function Prototypes
*****************************************************************************/

/*******************************************************************************
* Function Name: HandleUSBComm
********************************************************************************
* Summary:
* This function handles comm for sending data received by UART to USB.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
*  Clears the UART SW buffer if overflow is detected.
*
* Note:
*
*******************************************************************************/
extern void HandleUSBComm(void);

/*******************************************************************************
* Function Name: USBCommInit
********************************************************************************
* Summary:
* This function initializes rx state machine to its initial state and also
* initializes local buffers meant for receiving packets.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
* Modifies  pktRxState and Ep_Data
*
* Note:
*
*******************************************************************************/
extern void USBCommInit(void);

#if defined(USBFS_ENABLE_CDC_CLASS)

/*******************************************************************************
* Function Name: UARTTransmit()
********************************************************************************
* Summary:
* This function handles the USB-UART interface.
* It handles,
* a) Selection of UART clock based on host baud rate
* b) Transmit host packet to UART TX
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
* Clears the isCDCOutDataPresent flag
*
*******************************************************************************/
extern void UARTTransmit(void);

/*******************************************************************************
* Function Name: USBUARTInit()
********************************************************************************
* Summary:
* Starts the UART, sets Tx Pin to HiZ to save power during enumeration
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
extern void USBUARTInit(void);

/*******************************************************************************
* Function Name: USBUARTStart()
********************************************************************************
* Summary:
* Initializes CDC Interface, Sets Tx Pin to Strong Drive
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
extern void USBUARTStart(void);
#endif /*USBFS_ENABLE_CDC_CLASS*/

#endif /* _USB_INTERFACE_H_ */
